<?php

	require_once("Game.php");
	
	class  Battleground extends Game {

		private $dbh;
		private $idGame;
		private $idBG;
		private $idP1;
		private $idP2;
		private $battlefield;
		private $arrayP1;
		private $arrayP2;

		public function __construct($idGame, $idP1, $idP2, $ok) {
			$db = new Db();
			$this->dbh = $db->getDbh();
			$this->idGame =$idGame;
			$this->idBG = rand(0, 25648);
			$this->idP1 =$idP1;
			$this->idP2 =$idP2;
			$this->arrayP1 = array();
			$this->arrayP2 = array();
			$this->battlefield = array(1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E', 6 => 'F', 7 => 'G', 8 => 'H', 9 => 'I', 10 => 'J');
			if($ok == 1){
				$this->generateBattleground();
			}
		}
		public function jsonSerialize () {
			return json_encode(array(
				'idGame'=> $this->idGame,
				'idBG'=> $this->idBG,
				'idP1'=> $this->idP1,
				'idP2'=> $this->idP2
			));
		}
		public function __get($attr) {
			return $this->$attr;
		}

		public function generateBattleground() {
			$l = 0;
			for ($i=1; $i <= 10; $i++) { 
				for ($j=1 ; $j <= 10; $j++) { 
					$this->arrayP1[$l]['x'] = $this->battlefield[$j];
					$this->arrayP2[$l]['x'] = $this->battlefield[$j];
					$this->arrayP1[$l]['y'] = $i;
					$this->arrayP2[$l]['y'] = $i;
					$this->arrayP1[$l]['cas'] = 0;
					$this->arrayP2[$l]['cas'] = 0;
					$this->initBattleground($this->idP1, $this->idGame, $this->arrayP1[$l]['x'], $this->arrayP1[$l]['y']);
					$this->initBattleground($this->idP2, $this->idGame, $this->arrayP1[$l]['x'], $this->arrayP2[$l]['y']);
					$l ++;
				}
			}
		} 

		public function initBattleground($idPlayer, $idGame, $x, $y) {
			$sql = "INSERT INTO battleground (player_id, game_id, battleground_position_x, battleground_position_y, battleground_state) 
				VALUES(:idPlayer, :idGame, :x, :y, 0) ";
			$stmt = $this->dbh->prepare($sql);
			$stmt->bindValue(":idPlayer", $idPlayer);
			$stmt->bindValue(":idGame", $idGame);
			$stmt->bindValue(":x", $x);
			$stmt->bindValue(":y", $y);
			$stmt->execute();
		}

		public function positionBattleship($idPlayer, $idGame, $taille, $x, $y) {
			$bornesY = array(1 => 1, 2 => 10);
			$xPos = $xNeg = $x;
			$yPos = $yNeg = $y;
			$state = $this->checkPosition($idPlayer, $idGame, $x, $y);
			if($state == 0){
				$xPosOk = $xNegOk = $yPosOk = $yNegOk = 1;
				//for verification des placements sur les deux axe en + et -
				for ($i=1; $i <= $taille - 1; $i++) {
					if(array_keys($this->battlefield, $xPos) && $xPos != null && $xPosOk == 1){
						$shiftPos = array_keys($this->battlefield, $xPos);
						if($shiftPos != null){
							$indexXpos = $shiftPos[0];
							if( ($indexXpos+1) > 10 ){
								$xPosOk = 0;
							}
							else{
								$xPos = $this->battlefield[$indexXpos+1];
								$state = $this->checkPosition($idPlayer, $idGame, $xPos, $y);
								if ($state != 0) {
									$xPosOk = 0;
								}
							}
						}
					}
					else {
						$xPosOk = 0;
					}
					if(array_keys($this->battlefield, $xNeg) && $xNeg != null && $xNegOk == 1){
						$shiftNeg = array_keys($this->battlefield, $xNeg);
						if($shiftNeg != null){
							$indexXneg = $shiftNeg[0];
							if( ($indexXneg-1) <= 0 ){
								$xNegOk = 0;
							}
							else{
								$xNeg = $this->battlefield[$indexXneg-1];
								$state = $this->checkPosition($idPlayer, $idGame, $xNeg, $y);
								if ($state != 0) {
									$xNegOk = 0;
								}
							}
						}
					}
					else {
						$xNegOk = 0;
					}
					if($yPos >= $bornesY[1] && $yPos < $bornesY[2] && $yPosOk == 1){
						$yPos++;
						$state = $this->checkPosition($idPlayer, $idGame, $x, $yPos);
						if ($state != 0) {
							$yPosOk = 0;
						}
					}
					else{
						$yPosOk = 0;
					}
					if($yNeg > $bornesY[1] && $yNeg <= $bornesY[2] && $yNegOk == 1){
						$yNeg--;
						$state = $this->checkPosition($idPlayer, $idGame, $x, $yNeg);
						if ($state != 0) {
							$yNegOk = 0;
						}
					}
					else{
						$yNegOk = 0;
					}
				}
				//fin for*
			}
			else {
				$xPosOk = $xNegOk = $yPosOk = $yNegOk = 0;
			}
			$posValide = array( 'x' => $x, 'y' => $y, 'size' => $taille, 'xpos' => $xPosOk, 'xneg' => $xNegOk, 'ypos' => $yPosOk, 'yneg' => $yNegOk);
			return $posValide;
		}
                
                public function setStartPosition($idPlayer, $idGame, $arrayXY){
                    foreach($arrayXY as $position){
                    	$x = $position['x'];
                    	$y = $position['y'];
                        $sql = "UPDATE battleground
                                SET battleground_state = 1 
                                WHERE battleground_position_x = :x 
                                AND battleground_position_y = :y 
                                AND player_id = :idPlayer 
                                AND game_id = :idGame";
                        $stmt = $this->dbh->prepare($sql);
                        $stmt->bindValue(":x", $x);
                        $stmt->bindValue(":y", $y);
                        $stmt->bindValue(":idPlayer", $idPlayer);
                        $stmt->bindValue(":idGame", $idGame);
                        $stmt->execute();
                    }
                }
                
                public function checkPosition($idPlayer, $idGame, $x, $y){
                        $sql = "SELECT battleground_state
                                FROM battleground
                                WHERE battleground_position_x = :x 
                                AND battleground_position_y = :y 
                                AND player_id = :idPlayer 
                                AND game_id = :idGame";
                        $stmt = $this->dbh->prepare($sql);
                        $stmt->bindValue(":x", $x);
                        $stmt->bindValue(":y", $y);
                        $stmt->bindValue(":idPlayer", $idPlayer);
                        $stmt->bindValue(":idGame", $idGame);
                        $stmt->execute();
                        $pos = $stmt->fetch();
                        return intval($pos['battleground_state']);
                }

                
	}